package com.userentry;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.commonfunction.CommFunction;
import com.databasefunction.AllDBFunction;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UserEntrySubmitAction extends ActionSupport{
	
	private String name,role,address,city,district,state,country,zipcode,contact,email,date;
	private String userrole;
	
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getRole() {
		return role;
	}

	public void setRole(String role) {
		this.role = role;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getDistrict() {
		return district;
	}

	public void setDistrict(String district) {
		this.district = district;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	public String getZipcode() {
		return zipcode;
	}

	public void setZipcode(String zipcode) {
		this.zipcode = zipcode;
	}

	public String getContact() {
		return contact;
	}

	public void setContact(String contact) {
		this.contact = contact;
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		UserEntryFunction entryFunction = new UserEntryFunction();
		userrole = (String) ActionContext.getContext().getSession().get("userrole"); 
		if ((null != userrole) && (userrole.equals("deo") || userrole.equals("admin") || userrole.equals("superadmin"))) {
			
		}else
			return "unauthorised";
		
		if (!entryFunction.submitdata(name.replace(" ", "_"), role, address, city, district, state, country, zipcode, contact, email, date)) {
			
		}else{
			addActionMessage("Data submitted successfully");
		}
		return "success";
	}
	
	@Override
	public void validate() {
		// TODO Auto-generated method stub
		userrole = (String) ActionContext.getContext().getSession().get("userrole");
		CommFunction commFunction = new CommFunction();
		AllDBFunction allDBFunction = new AllDBFunction();
		
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("ddmmyyyy");
		Date dateToday = new Date();
		date = simpleDateFormat.format(dateToday);
		if (allDBFunction.isdealerexists(name)) {
			addActionError("Please use different userid");
			System.out.println("In userentrySubmitAction......Id present");
		}
	}
}
